package com.example.demo_springboot.redis;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class Receiver{
    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String message) {
        //启动应用redis 采用发布者/订阅者消息模式
        //测试方法:
        //1.redis-server.exe --service-start //启动redis服务
        //2.启动DemoStpringbootApplication引用,即启动程序订阅者监听
        //3.另开一个dos连接redis服务 -> redis-cli.exe -h localhost -p 6379 并且订阅chat频道 -> subscribe chat
        //4.另开一个dos连接redis服务 -> redis-cli.exe -h localhost -p 6379 并且发布chat消息 -> publish chat "消息体内容"
        //5.程序应用控制台输出如下, 同时订阅chat频道的dos窗口也会同时输出发布消息内容
//        2020-08-14 15:13:14.324  INFO 126928 --- [  restartedMain] c.e.d.DemoSpringbootApplication          : Sending message...
//        2020-08-14 15:13:14.495  INFO 126928 --- [    container-2] c.e.demo_springboot.redis.Receiver       : Received <Hello from Redis!>
//                2020-08-14 15:14:28.939  INFO 126928 --- [    container-3] c.e.demo_springboot.redis.Receiver       : Received <world>
//        2020-08-14 15:15:16.893  INFO 126928 --- [    container-4] c.e.demo_springboot.redis.Receiver       : Received <zhognwenxiaoxi >
//        2020-08-14 15:15:56.544  INFO 126928 --- [    container-5] c.e.demo_springboot.redis.Receiver       : Received <en>
//        2020-08-14 15:16:11.912  INFO 126928 --- [    container-6] c.e.demo_springboot.redis.Receiver       : Received <1000>
        LOGGER.info("Received <" + message + ">");
//        latch.countDown();
    }
}